//
// Created by wxy on 24-12-15.
//

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "sys/ioctl.h"

#define CLOSE_CMD _IO(0xEF, 1)     // close the timer
#define OPEN_CMD _IO(0xEF, 2)      // Open the timer
#define SETPERIOD_CMD _IO(0xEF, 3) // Set the period of the timer

int main(int argc, char *argv[])
{
    int fd, ret;
    char *filename;
    unsigned int cmd;
    unsigned int arg;
    filename = argv[1];
    unsigned char str[100];

    if (argc != 2)
    {
        printf("ERROR : Invalid\r\n");
        return -1;
    }
    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("file %s is open fail\r\n", filename);
        return -1;
    }
    else
    {
        printf("file %s is open success\r\n", filename);
    }

    while (1)
    {
        printf("Please input command:\r\n");
        ret = scanf("%d", &cmd);
        if (ret != 1)
        {
            gets(str);
        }
        if (cmd == 1)
        {
            cmd = CLOSE_CMD;
        }
        else if (cmd == 2)
        {
            cmd = OPEN_CMD;
        }
        else if (cmd == 3)
        {
            cmd = SETPERIOD_CMD;
            printf("Please input the period :\r\n");
            ;
            ret = scanf("%d", &arg);
            if (ret != 1)
            {
                gets(str);
            }
        }
        ioctl(fd, cmd, arg);
    }
    close(fd);
    return 0;
}